//
//  FindViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "FindViewController.h"
#import "FitHeader.h"
#import "TitleCollectionViewCell.h"
#import "ContentCollectionViewCell.h"
#import "BillboardCollectionViewCell.h"
#import "RecommendCollectionViewCell.h"
#import "LiveCollectionViewCell.h"
#import "ZBBLocationViewController.h"//直播里面的本地台
#import "ZBBProvinceViewController.h"
#import "ZBBPlayHistory.h"
#import "ZBBPlayHistoryViewController.h"
@interface FindViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate, ZBBJumpToDetailDelegate>
@property(nonatomic,strong)UICollectionView *titleCollectionView;
@property(nonatomic,strong)UICollectionView *contentCollectionView;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,assign)NSInteger lineColorFlag;

@end

@implementation FindViewController

-(void)loadView{
    [super loadView];
    
    _lineColorFlag = 0;
    _titleArray = [NSArray arrayWithObjects:@"推荐", @"分类", @"直播", @"榜单", @"主播", nil];
    UICollectionViewFlowLayout *titleFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    titleFlowLayout.minimumInteritemSpacing = 0;
    titleFlowLayout.minimumLineSpacing = 0;
    titleFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    titleFlowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 5, 50 * FITHeight);
    _titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50 * FITHeight) collectionViewLayout:titleFlowLayout];
    [_titleCollectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    _titleCollectionView.pagingEnabled = NO;
    _titleCollectionView.dataSource = self;
    _titleCollectionView.delegate = self;
    _titleCollectionView.layer.borderWidth = 0.5;
    _titleCollectionView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.view addSubview:_titleCollectionView];
    
    UICollectionViewFlowLayout *contentFlowlayout = [[UICollectionViewFlowLayout alloc] init];
    contentFlowlayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - 50 * FITHeight);
    contentFlowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    contentFlowlayout.minimumInteritemSpacing = 0;
    contentFlowlayout.minimumLineSpacing = 0;
    contentFlowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.contentCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _titleCollectionView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - 64 - 49 - 50 * FITHeight) collectionViewLayout:contentFlowlayout];
    //推荐注册
    [_contentCollectionView registerClass:[RecommendCollectionViewCell class] forCellWithReuseIdentifier:@"recommendIdentifier"];
    
    //榜单注册
    [_contentCollectionView registerClass:[BillboardCollectionViewCell class] forCellWithReuseIdentifier:@"billBoardIdentifier"];
    
    [_contentCollectionView registerClass:[LiveCollectionViewCell class] forCellWithReuseIdentifier:@"live"];
    
    _contentCollectionView.dataSource = self;
    _contentCollectionView.delegate = self;
    _contentCollectionView.pagingEnabled = YES;
    _contentCollectionView.backgroundColor = [UIColor whiteColor];
    _contentCollectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_contentCollectionView];
    
}
#pragma mark 朱彬彬的代理方法, 可以跳转到下一页(参数是index.row)
-(void)PushToDetail:(NSInteger)index
{
    
    switch (index) {
        case 0:
        case 1:
        case 3:
        {
       ZBBLocationViewController *zbblvc = [[ZBBLocationViewController alloc]init];
       zbblvc.index = index;
       [self.navigationController pushViewController:zbblvc animated:YES];
        }
        break;
        case 2:
        {
        ZBBProvinceViewController *province = [[ZBBProvinceViewController alloc]init];
        [self.navigationController pushViewController:province animated:YES];
        }
        break;
        case 1000:
        {
  ZBBLocationViewController *zbblvc = [[ZBBLocationViewController alloc]init];
    zbblvc.index = index;
     [self.navigationController pushViewController:zbblvc animated:YES];
        }
            break;
        case 1001:
        {
      
            ZBBPlayHistoryViewController *history = [[ZBBPlayHistoryViewController alloc]init];
            [self.navigationController pushViewController:history animated:YES];
        }
        break;
        default:
        break;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发现";
    self.navigationController.navigationBar.translucent = NO;
   
}

#pragma mark --scrollView代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _contentCollectionView) {
        _lineColorFlag = _contentCollectionView.contentOffset.x / self.view.frame.size.width;
        [_titleCollectionView reloadData];
    }
}


#pragma mark --collectionView代理方法

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _titleCollectionView) {
        TitleCollectionViewCell *titleCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
        titleCell.titleLabel.text = [_titleArray objectAtIndex:indexPath.row];
        titleCell.lineLabel.backgroundColor = [UIColor whiteColor];
        titleCell.titleLabel.textColor = [UIColor blackColor];
        if (indexPath.row == _lineColorFlag) {
            titleCell.lineLabel.backgroundColor = [UIColor orangeColor];
            titleCell.titleLabel.textColor = [UIColor orangeColor];
        }
        return titleCell;
    }
    else{
        if (indexPath.row == 0) {
            RecommendCollectionViewCell *recommendCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"recommendIdentifier" forIndexPath:indexPath];
            recommendCell.s1mpl3Nav = _nav;
            return recommendCell;
        }

        else if (indexPath.row == 3) {
            BillboardCollectionViewCell *billBoardCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"billBoardIdentifier" forIndexPath:indexPath];
            billBoardCell.nav = _nav;
            return billBoardCell;
        }
        if (indexPath.row == 2)
        {
         LiveCollectionViewCell *liveCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"live" forIndexPath:indexPath];
            liveCell.delegate = self;
            liveCell.playHistoryArray = [[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"ppppp"];
            [liveCell addArray:[[ZBBPlayHistory shareInstance] takeHistoryArrayFromFile:@"ppppp"]];
           return liveCell;
        }
    }
    return nil;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _titleCollectionView) {
        _lineColorFlag = indexPath.row;
        _contentCollectionView.contentOffset = CGPointMake(_lineColorFlag * self.view.frame.size.width, 0);
        [_titleCollectionView reloadData];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return _titleArray.count;
}
-(void)viewWillAppear:(BOOL)animated
{
    [_contentCollectionView reloadData];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
